package com.leo.component;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import com.almasb.fxgl.entity.components.ViewComponent;
import com.almasb.fxgl.texture.Texture;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-05  23:25
 * @Description: 自定义坦克组件
 */
public class TankComponent extends Component {

    // 这种方式框架会自动注入，但是在entityfactory中一定要在TankComponent前边加入
    private SayHelloComponent sc;

    /**
     * 当组件添加到实体的时候会被调用
     */
    @Override
    public void onAdded() {
        ViewComponent viewComponent = entity.getViewComponent();
        viewComponent.addOnClickHandler(mouseEvent -> {
            Texture node = (Texture) viewComponent.getChildren().get(0);
            node.dispose();
            viewComponent.clearChildren();
            viewComponent.addChild(FXGL.texture("tank/tank.png").multiplyColor(FXGLMath.randomColor()));
            // 这中方式可以防止 SayHelloComponent 对象不存在
            entity.getComponent(SayHelloComponent.class).sayHello();
        });
    }

    /**
     * 每一帧都会更新
     *
     * @param tpf
     */
    @Override
    public void onUpdate(double tpf) {

    }
}
